package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.tm.model.entity.Technology;
import com.erpanderp.wam.tm.model.service.TechnologyDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateSAPERPModuleBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private SAPERPModule sapErpModule = new SAPERPModule();
	private Technology technology = new Technology();
	SAPERPModuleDAO sapERPModuleDAO = new SAPERPModuleDAO();
	TechnologyDAO technologyDAO = new TechnologyDAO();
	private boolean viewSAPERPModulesList;
	private List<SAPERPModule> sapERPModules = new ArrayList<SAPERPModule>();
	private SAPERPModule[] selectedSAPERPModules;
	private boolean FORM_VALID;
	
	private String assignViewSAPERPModulesButtonLabel = CommonLookup.ASSIGN_VIEW_SPAERP_MODULE_LABEL;
	
	
	
	public void saveSapErpModule(ActionEvent e){	
		validateForm();
		if(FORM_VALID){
			sapErpModule.setCreateDate(new Date());
			sapErpModule.setCreatedBy( "DEVELOPER");
			sapErpModule.setStatus("ACTIVE");
			sapERPModuleDAO.createSAPERPModule(sapErpModule);
			
			technology.setTechnology(sapErpModule.getSAPERPModule());
			technology.setDisplayName(sapErpModule.getDisplayName());
			technology.setDisplayOrder(sapErpModule.getDisplayOrder());
			technology.setDescription(sapErpModule.getDescription());
			technology.setCreateDate(new Date());
			technology.setCreatedBy( "DEVELOPER");
			technology.setStatus("ACTIVE");
			technologyDAO.createTechnology(technology);
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, sapErpModule.getSAPERPModule() + " module created successfully.", ""));
			sapErpModule = new SAPERPModule();
		}else{
			
		}
		System.out.println("IN SAVE MODULE");
	}
	
	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(sapErpModule.getSAPERPModule(), "Module Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(sapErpModule.getDisplayName(), "Display Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(sapErpModule.getDisplayOrder().toString(), "Display Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
		
	}
    
	public void toggleAssignPanelDisplay(ActionEvent e){
		
		System.out.println("In toggleAssignPanelDisplay");
	
		if(assignViewSAPERPModulesButtonLabel.equals(CommonLookup.ASSIGN_VIEW_SPAERP_MODULE_LABEL)){
			setAssignViewSAPERPModulesButtonLabel(CommonLookup.DO_NOT_VIEW_SPAERP_MODULE_LABEL);
			setViewSAPERPModulesList(true);
			setSapERPModules(sapERPModuleDAO.getSAPERPModules());			
		}else{
			setAssignViewSAPERPModulesButtonLabel(CommonLookup.ASSIGN_VIEW_SPAERP_MODULE_LABEL);
			setViewSAPERPModulesList(false);
		}		
   }
	
	//SETTER AND GETER METHODS
	
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public SAPERPModule getSapErpModule() {
		return sapErpModule;
	}

	public void setSapErpModule(SAPERPModule sapErpModule) {
		this.sapErpModule = sapErpModule;
	}

	public String getAssignViewSAPERPModulesButtonLabel() {
		return assignViewSAPERPModulesButtonLabel;
	}

	public void setAssignViewSAPERPModulesButtonLabel(
			String assignViewSAPERPModulesButtonLabel) {
		this.assignViewSAPERPModulesButtonLabel = assignViewSAPERPModulesButtonLabel;
	}

	public boolean isViewSAPERPModulesList() {
		return viewSAPERPModulesList;
	}

	public void setViewSAPERPModulesList(boolean viewSAPERPModulesList) {
		this.viewSAPERPModulesList = viewSAPERPModulesList;
	}

	

	public List<SAPERPModule> getSapERPModules() {
		return sapERPModules;
	}

	public void setSapERPModules(List<SAPERPModule> sapERPModules) {
		this.sapERPModules = sapERPModules;
	}

	public SAPERPModule[] getSelectedSAPERPModules() {
		return selectedSAPERPModules;
	}

	public void setSelectedSAPERPModules(SAPERPModule[] selectedSAPERPModules) {
		this.selectedSAPERPModules = selectedSAPERPModules;
	}

	public Technology getTechnology() {
		return technology;
	}

	public void setTechnology(Technology technology) {
		this.technology = technology;
	}

}
